package com.ascentive.finallyfast.deepclean;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ascentive.finallyfast.R;

public class DeepCleanListExpandableAdapter extends BaseExpandableListAdapter {

	private static final String LOG_TAG = "EmptyGroupsAdapter";
	private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
         EMPTY_STATE_SET, // 0
         GROUP_EXPANDED_STATE_SET // 1
	};
    
	private Context context;
    private ArrayList<DeepCleanListItem> groups;
    private ArrayList<ArrayList<DeepCleanListItem>> children;
    private LayoutInflater inflater;
    private DeepCleanListAdapterListener mListener;
    
	public DeepCleanListExpandableAdapter(Context context,
			ArrayList<DeepCleanListItem> groups, ArrayList<ArrayList<DeepCleanListItem>> children) {
		this.context = context;
		this.groups = groups;
		this.children = children;
		
		inflater = LayoutInflater.from(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		if  (children.get( groupPosition) != null)  //&& children.get( groupPosition ).get(childPosition))
			return children.get( groupPosition ).get( childPosition );
		else
			return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int cpos = childPosition;
		final int gpos = groupPosition;
		View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.deep_clean_list_row, parent, false); 
        DeepCleanListItem c = (DeepCleanListItem)getChild( groupPosition, childPosition );
		ImageView icon = (ImageView)v.findViewById( R.id.image);
		if( icon != null )
			icon.setImageResource(c.getImage());
		
		TextView categorytxt = (TextView)v.findViewById( R.id.txt );
		if( categorytxt != null )
			categorytxt.setText( c.getItemName() );
		
		CheckBox checkBox = (CheckBox)v.findViewById( R.id.chk );
		checkBox.setTag(c);
		checkBox.setChecked(c.isCheckBoxValue());
		checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		    @Override
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		    	DeepCleanListItem c = (DeepCleanListItem)buttonView.getTag();
		    	c.setCheckBoxValue(isChecked);
		    	
				if(isChecked)
					Log.v(null, "Child checked "+ c.getItemName());
				else
					Log.v(null, "Child un checked "+ c.getItemName());
				notifyDataSetChanged();
					
				// mListener.onItemChecked(c);
		    }
		});
       
        return v;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		
		if( (children != null) && children.get( groupPosition ) != null) 
			return children.get( groupPosition ).size();
		else return 0;
	}

	public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);

        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }
	
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
        return groups.get( groupPosition );
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
        return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
	
		final int pos = groupPosition;
    	DeepCleanListItem c = (DeepCleanListItem)getGroup( groupPosition);
		
		View groupRow = null;
		if (convertView == null) {
		    // LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    groupRow = inflater.inflate(R.layout.deep_clean_list_row_group, parent, false);
		} else {
		    groupRow = convertView;
		}
		groupRow.setTag(c);
		ImageView icon = (ImageView) groupRow.findViewById( R.id.image);
		if( icon != null )
			icon.setImageResource(c.getImage());
		
		ImageView indicatorIcon = (ImageView) groupRow.findViewById(R.id.explist_indicator);
		if( indicatorIcon != null ) {
			ImageView indicator = (ImageView)indicatorIcon;
			// Log.d(LOG_TAG, "groupPosition: "+ groupPosition);
			
			if( getChildrenCount( groupPosition ) == 0 ) {
				indicator.setVisibility( View.INVISIBLE );
			} else {
				indicator.setVisibility( View.VISIBLE );
				int stateSetIndex = ( isExpanded ? 1 : 0) ;
				Drawable drawable = indicator.getDrawable();
				drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
			}
		}
		
		TextView title = (TextView) groupRow.findViewById(R.id.txt);
		title.setText(c.getItemName()); //  getGroup(groupPosition).toString());
		CheckBox checkBox = (CheckBox) groupRow.findViewById(R.id.chk);
		checkBox.setTag(c);
		checkBox.setChecked(c.isCheckBoxValue());
		checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
		    @Override
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		    	DeepCleanListItem ee = (DeepCleanListItem)buttonView.getTag();
		    	ee.setCheckBoxValue(isChecked);
    			
		    	for(int i = 0; i < groups.size(); i++){
		    		DeepCleanListItem item =  groups.get(i);
		    		if(item.equals(ee)){
		    			ArrayList<DeepCleanListItem> list = children.get(i);
		    			if(list == null)
		    				continue;
		    			for(DeepCleanListItem child: list){
		    				child.setCheckBoxValue(isChecked);
		    			}
		    			notifyDataSetChanged();
		    			break;
		    		}
		    	}
				
				// mListener.onItemChecked(groups.get(pos));
				// mListener.onItemChecked(children.get(pos));
		    }
		});

		return groupRow;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	public void setListener(DeepCleanListAdapterListener listener){
		mListener = listener;
	}
	
}
